﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HalconDotNet;
using ImageControl;

namespace ImageShow
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private HObject final_region = new HObject();//需要获得的区域
        private int penSize = 5;
        private int paintType = 0;

        private bool _ShowROI = false;
        public bool ShowROI
        {
            get { return _ShowROI; }
            set
            {
                _ShowROI = value;
                OnPropertyChanged();
                if (this.hWnd.HalconWnd != null && this.hWnd.HalconWnd.IsInitialized())
                {
                    if (_ShowROI)
                    {
                        dtool.AttachAllDrawObj(this.hWnd.HalconWnd);
                    }
                    else
                    {
                        dtool.DetachDrawObjects(this.hWnd.HalconWnd);
                    }
                }

            }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            this.hWnd.SmartCtrlWPF.HMouseDown += SmartCtrlWPF_HMouseDown;
            this.hWnd.SmartCtrlWPF.HMouseUp += SmartCtrlWPF_HMouseUp;
            this.hWnd.SmartCtrlWPF.HMouseMove += SmartCtrlWPF_HMouseMove;
            dtool.DrawItem(DrawingType.Rectangle2, "ROI", "red");
        }

        private void SmartCtrlWPF_HMouseMove(object sender, HSmartWindowControlWPF.HMouseEventArgsWPF e)
        {
            this.hWnd.HalconWnd.ConvertCoordinatesWindowToImage(e.Y, e.X, out hv_Row, out hv_Column);
            PaintRegion();
        }

        private void SmartCtrlWPF_HMouseUp(object sender, HSmartWindowControlWPF.HMouseEventArgsWPF e)
        {
            hv_button = 0;
        }

        private void SmartCtrlWPF_HMouseDown(object sender, HSmartWindowControlWPF.HMouseEventArgsWPF e)
        {
            if (e.Button == MouseButton.Left)
            {
                hv_button = 1;
            }
            else
            {
                hv_button = 0;
            }
        }

        private DrawTool dtool = new DrawTool();

        private void ReadImage_Click(object sender, RoutedEventArgs e)
        {
            final_region?.Dispose();
            HImage img1 = new HImage("fabrik");
            HImage img2 = new HImage("a02");
            this.hWnd.WindowCtrl.ClearAllList();
            this.hWnd.AddImage(new HWndImage(img1, "TEST1"));
            this.hWnd.AddImage(new HWndImage(img2, "TEST2"));
            this.hWnd.AddText(new HWndMessage("OK", 10, 100));
            this.hWnd.AddText(new HWndMessage("NG", 80, 130, 30, "red"));
            this.hWnd.FitImageWindow();
        }

        private void PaintAdd_Click(object sender, RoutedEventArgs e)
        {
            paintType = 1;
            this.hWnd.WindowCtrl.CanMove = false;
        }

        private void PaintDec_Click(object sender, RoutedEventArgs e)
        {
            paintType = 2;
            this.hWnd.WindowCtrl.CanMove = false;
        }
        private void CancelPaint_Click(object sender, RoutedEventArgs e)
        {
            paintType = 0;
            this.hWnd.WindowCtrl.CanMove = true;
        }

        double hv_Row, hv_Column;
        int hv_button;

        /// <summary>
        /// 涂抹擦除
        /// </summary>
        private void PaintRegion()
        {
            if (this.hWnd.WindowCtrl.HImage == null || !this.hWnd.WindowCtrl.HImage.IsInitialized())
            {
                return;
            }
            //this.hWnd.HalconWindow.GetMbutton(out int Row, out int Column, out int hv_Button);
            if (paintType > 0)
            {
                if (final_region.IsInitialized())
                {
                    HObject ho_PaintImage = new HObject();
                    HObject ho_AddImage = new HObject();
                    HObject ho_MultiChannelImage = new HObject();
                    ho_PaintImage.Dispose();
                    ho_AddImage.Dispose();
                    ho_MultiChannelImage.Dispose();
                    HOperatorSet.PaintRegion(final_region, this.hWnd.WindowCtrl.HImage, out ho_PaintImage, 255, "fill");
                    HOperatorSet.AddImage(this.hWnd.WindowCtrl.HImage, ho_PaintImage, out ho_AddImage, 0.5, 0);
                    HOperatorSet.Compose3(ho_AddImage, this.hWnd.WindowCtrl.HImage, this.hWnd.WindowCtrl.HImage, out ho_MultiChannelImage);
                    this.hWnd.WindowCtrl.ClearHObjects();
                    this.hWnd.AddObject(new HWndObject(ho_MultiChannelImage));
                    this.hWnd.Repaint();
                    ho_PaintImage.Dispose();
                    ho_AddImage.Dispose();
                }
                if (hv_Row >= 0 && hv_Column >= 0)
                {

                    HObject brush = null;
                    HOperatorSet.GenEmptyObj(out brush);
                    brush.Dispose();
                    HOperatorSet.GenCircle(out brush, hv_Row, hv_Column, penSize);
                    //1为鼠标左键
                    if (hv_button == 1)
                    {
                        //画出笔刷
                        switch (paintType)
                        {
                            case 1:
                                {
                                    if (final_region.IsInitialized())
                                    {
                                        HObject ExpTmpOutVar_0;
                                        HOperatorSet.Union2(final_region, brush, out ExpTmpOutVar_0);
                                        final_region.Dispose();
                                        final_region = ExpTmpOutVar_0;
                                    }
                                    else
                                    {
                                        final_region = new HObject(brush);
                                    }
                                }
                                break;
                            case 2:
                                {
                                    HObject ExpTmpOutVar_0;
                                    HOperatorSet.Difference(final_region, brush, out ExpTmpOutVar_0);
                                    final_region.Dispose();
                                    final_region = ExpTmpOutVar_0;
                                }
                                break;
                            default:
                                break;
                        }//end switch


                    }//end if
                }

            }

        }


    }
}
